<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="继续第一天学习：">
<meta property="og:type" content="article">
<meta property="og:title" content="rust学习笔记（2）：Day1 Afternoon">
<meta property="og:url" content="https://wyatex.online/Rust/rust-note-2-google/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="继续第一天学习：">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-01-02T08:00:25.000Z">
<meta property="article:modified_time" content="2023-05-05T01:25:47.239Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="学习笔记">
<meta property="article:tag" content="Rust">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/Rust/rust-note-2-google/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/Rust/rust-note-2-google/","path":"Rust/rust-note-2-google/","title":"rust学习笔记（2）：Day1 Afternoon"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>rust学习笔记（2）：Day1 Afternoon | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%98%E9%87%8F"><span class="nav-number">1.</span> <span class="nav-text">变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E5%9E%8B%E6%8E%A8%E5%AF%BC"><span class="nav-number">1.1.</span> <span class="nav-text">类型推导</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%99%E6%80%81%E5%8F%98%E9%87%8F%E5%92%8C%E6%81%92%E5%8F%98%E9%87%8F-Static-and-Constant-Variables"><span class="nav-number">1.2.</span> <span class="nav-text">静态变量和恒变量 &#x2F; Static and Constant Variables</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#const"><span class="nav-number">1.2.1.</span> <span class="nav-text">const</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#static"><span class="nav-number">1.2.2.</span> <span class="nav-text">static</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Scopes-and-Shadowing"><span class="nav-number">1.3.</span> <span class="nav-text">Scopes and Shadowing</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.</span> <span class="nav-text">内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A0%86%E5%92%8C%E6%A0%88-The-Stack-vs-The-Heap"><span class="nav-number">2.1.</span> <span class="nav-text">堆和栈 &#x2F; The Stack vs The Heap</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%88%E5%86%85%E5%AD%98"><span class="nav-number">2.2.</span> <span class="nav-text">栈内存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%8B%E5%8A%A8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.3.</span> <span class="nav-text">手动内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#C%E8%AF%AD%E8%A8%80%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="nav-number">2.3.1.</span> <span class="nav-text">C语言的例子</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E4%BA%8E%E4%BD%9C%E7%94%A8%E5%9F%9F%E7%9A%84%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.4.</span> <span class="nav-text">基于作用域的内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#C-%E4%BE%8B%E5%AD%90"><span class="nav-number">2.4.1.</span> <span class="nav-text">C++例子</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.5.</span> <span class="nav-text">自动内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Java%E4%BE%8B%E5%AD%90"><span class="nav-number">2.5.1.</span> <span class="nav-text">Java例子</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Rust%E4%B8%AD%E7%9A%84%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.6.</span> <span class="nav-text">Rust中的内存管理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AF%B9%E6%AF%94"><span class="nav-number">3.</span> <span class="nav-text">对比</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8D%E5%90%8C%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%8A%80%E6%9C%AF%E7%9A%84%E4%BC%98%E7%82%B9"><span class="nav-number">3.1.</span> <span class="nav-text">不同内存管理技术的优点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8D%E5%90%8C%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%8A%80%E6%9C%AF%E7%9A%84%E7%BC%BA%E7%82%B9"><span class="nav-number">3.2.</span> <span class="nav-text">不同内存管理技术的缺点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%80%E6%9C%89%E6%9D%83"><span class="nav-number">4.</span> <span class="nav-text">所有权</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E7%A7%BB%E8%AF%AD%E4%B9%89"><span class="nav-number">4.1.</span> <span class="nav-text">转移语义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8-Rust-%E4%B8%AD%E7%A7%BB%E5%8A%A8%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">4.2.</span> <span class="nav-text">在 Rust 中移动的字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9C%A8%E7%8E%B0%E4%BB%A3C-%E4%B8%AD%E9%87%8D%E5%A4%8Dfree"><span class="nav-number">4.2.1.</span> <span class="nav-text">在现代C++中重复free</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E4%B8%AD%E7%9A%84%E7%A7%BB%E5%8A%A8"><span class="nav-number">4.3.</span> <span class="nav-text">函数调用中的移动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%8D%E5%88%B6%E4%B8%8E%E5%85%8B%E9%9A%86"><span class="nav-number">4.4.</span> <span class="nav-text">复制与克隆</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%80%9F%E7%94%A8-Borrowing"><span class="nav-number">4.5.</span> <span class="nav-text">借用 &#x2F; Borrowing</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%85%B1%E4%BA%AB%E5%92%8C%E7%8B%AC%E7%89%B9%E7%9A%84%E5%80%9F%E7%94%A8"><span class="nav-number">4.5.1.</span> <span class="nav-text">共享和独特的借用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">4.6.</span> <span class="nav-text">生命周期</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E4%B8%AD%E7%9A%84%E7%94%9F%E5%AD%98%E6%9C%9F"><span class="nav-number">4.7.</span> <span class="nav-text">函数调用中的生存期</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88Data-Structures%EF%BC%89%E4%B8%AD%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">4.8.</span> <span class="nav-text">数据结构（Data Structures）中的生命周期</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0"><span class="nav-number">5.</span> <span class="nav-text">练习</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E8%AE%A1%E5%BA%93"><span class="nav-number">5.1.</span> <span class="nav-text">设计库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%92%8C%E6%89%80%E6%9C%89%E6%9D%83-%E9%9A%BE"><span class="nav-number">5.2.</span> <span class="nav-text">迭代器和所有权(难)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Iterator"><span class="nav-number">5.2.1.</span> <span class="nav-text">Iterator</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#IntoIterator"><span class="nav-number">5.2.2.</span> <span class="nav-text">IntoIterator</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#for%E5%BE%AA%E7%8E%AF"><span class="nav-number">5.2.3.</span> <span class="nav-text">for循环</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/Rust/rust-note-2-google/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="rust学习笔记（2）：Day1 Afternoon | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          rust学习笔记（2）：Day1 Afternoon
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-01-02 16:00:25" itemprop="dateCreated datePublished" datetime="2023-01-02T16:00:25+08:00">2023-01-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-05-05 09:25:47" itemprop="dateModified" datetime="2023-05-05T09:25:47+08:00">2023-05-05</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Rust/" itemprop="url" rel="index"><span itemprop="name">Rust</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>继续第一天学习：</p>
<span id="more"></span>

<h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><p>Rust通过静态类型提供类型安全性。默认情况下变量绑定是不可变的：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">x</span>: <span class="type">i32</span> = <span class="number">10</span>;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;x: &#123;x&#125;&quot;</span>);</span><br><span class="line">    <span class="comment">// x = 20; 这里如果重新赋值就会报错</span></span><br><span class="line">    <span class="comment">// println!(&quot;x: &#123;x&#125;&quot;);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="类型推导"><a href="#类型推导" class="headerlink" title="类型推导"></a>类型推导</h3><p>Rust将根据如何使用变量来确定类型：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">takes_u32</span>(x: <span class="type">u32</span>) &#123;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;u32: &#123;x&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">takes_i8</span>(y: <span class="type">i8</span>) &#123;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;i8: &#123;y&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">x</span> = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">y</span> = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">    <span class="title function_ invoke__">takes_u32</span>(x);</span><br><span class="line">    <span class="title function_ invoke__">takes_i8</span>(y);</span><br><span class="line">    <span class="comment">// takes_u32(y); // expected `u32`, found `i8`</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="静态变量和恒变量-Static-and-Constant-Variables"><a href="#静态变量和恒变量-Static-and-Constant-Variables" class="headerlink" title="静态变量和恒变量 &#x2F; Static and Constant Variables"></a>静态变量和恒变量 &#x2F; Static and Constant Variables</h3><p>全局状态由静态变量和常量变量管理。</p>
<h4 id="const"><a href="#const" class="headerlink" title="const"></a>const</h4><p>您可以声明编译时常量：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> DIGEST_SIZE: <span class="type">usize</span> = <span class="number">3</span>;</span><br><span class="line"><span class="keyword">const</span> ZERO: <span class="type">Option</span>&lt;<span class="type">u8</span>&gt; = <span class="title function_ invoke__">Some</span>(<span class="number">42</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">compute_digest</span>(text: &amp;<span class="type">str</span>) <span class="punctuation">-&gt;</span> [<span class="type">u8</span>; DIGEST_SIZE] &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">digest</span> = [ZERO.<span class="title function_ invoke__">unwrap_or</span>(<span class="number">0</span>); DIGEST_SIZE];</span><br><span class="line">    <span class="keyword">for</span> (idx, &amp;b) <span class="keyword">in</span> text.<span class="title function_ invoke__">as_bytes</span>().<span class="title function_ invoke__">iter</span>().<span class="title function_ invoke__">enumerate</span>() &#123;</span><br><span class="line">        digest[idx % DIGEST_SIZE] = digest[idx % DIGEST_SIZE].<span class="title function_ invoke__">wrapping_add</span>(b);</span><br><span class="line">    &#125;</span><br><span class="line">    digest</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">digest</span> = <span class="title function_ invoke__">compute_digest</span>(<span class="string">&quot;Hello&quot;</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Digest: &#123;digest:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="static"><a href="#static" class="headerlink" title="static"></a>static</h4><p>您还可以声明静态变量:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> BANNER: &amp;<span class="type">str</span> = <span class="string">&quot;Welcome to RustOS 3.14&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;BANNER&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们将在不安全Rust一章中介绍静态数据的突变。</p>
<h3 id="Scopes-and-Shadowing"><a href="#Scopes-and-Shadowing" class="headerlink" title="Scopes and Shadowing"></a>Scopes and Shadowing</h3><p>你可以隐藏变量，包括来自外部作用域的变量和来自同一作用域的变量：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">fn <span class="title function_">main</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> a = <span class="number">10</span>;</span><br><span class="line">    println!(<span class="string">&quot;before: &#123;a&#125;&quot;</span>);</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">let</span> a = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        println!(<span class="string">&quot;inner scope: &#123;a&#125;&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> a = <span class="literal">true</span>;</span><br><span class="line">        println!(<span class="string">&quot;shadowed in inner scope: &#123;a&#125;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    println!(<span class="string">&quot;after: &#123;a&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="内存管理"><a href="#内存管理" class="headerlink" title="内存管理"></a>内存管理</h2><p>传统上，语言可以分为两大类：</p>
<ul>
<li>通过手动内存管理完全控制:C, C++, Pascal…</li>
<li>运行时通过自动内存管理实现完全安全:Java, Python, Go, Haskell…</li>
</ul>
<p>Rust 提供了一个新的组合：</p>
<blockquote>
<p>完全控制和安全通过编译时执行正确的内存管理。</p>
</blockquote>
<p>它通过一个明确的所有权概念来做到这一点。首先，让我们回顾一下内存管理是如何工作的。</p>
<h3 id="堆和栈-The-Stack-vs-The-Heap"><a href="#堆和栈-The-Stack-vs-The-Heap" class="headerlink" title="堆和栈 &#x2F; The Stack vs The Heap"></a>堆和栈 &#x2F; The Stack vs The Heap</h3><ul>
<li>栈:局部变量的连续内存区域。<ul>
<li>值在编译时具有固定的大小。</li>
<li>非常快:只需移动一个栈指针。</li>
<li>易于管理:遵循函数调用。</li>
<li>完全的内存局部性。</li>
</ul>
</li>
<li>堆:函数调用之外的值的存储。<ul>
<li>值具有在运行时确定的动态大小。</li>
<li>比栈稍慢:需要向系统借取。</li>
<li>不保证内存局部性。</li>
</ul>
</li>
</ul>
<h3 id="栈内存"><a href="#栈内存" class="headerlink" title="栈内存"></a>栈内存</h3><p>创建Strings时将固定大小的数据放在堆栈上，并将动态大小的数据放在堆上：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">s1</span> = <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Hello&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://google.github.io/comprehensive-rust/memory-management/stack.html">这里有个图</a></p>
<h3 id="手动内存管理"><a href="#手动内存管理" class="headerlink" title="手动内存管理"></a>手动内存管理</h3><p>您自己分配和释放堆内存。</p>
<h4 id="C语言的例子"><a href="#C语言的例子" class="headerlink" title="C语言的例子"></a>C语言的例子</h4><p>你必须对使用<code>malloc</code>分配的每个指针调用<code>free</code>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">foo</span><span class="params">(<span class="type">size_t</span> n)</span> &#123;</span><br><span class="line">    <span class="type">int</span>* int_array = (<span class="type">int</span>*)<span class="built_in">malloc</span>(n * <span class="keyword">sizeof</span>(<span class="type">int</span>));</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">// ... lots of code</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="built_in">free</span>(int_array);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果函数在<code>malloc</code>和<code>free</code>之间提前返回，内存就会泄漏:指针丢失，我们无法释放内存。</p>
<h3 id="基于作用域的内存管理"><a href="#基于作用域的内存管理" class="headerlink" title="基于作用域的内存管理"></a>基于作用域的内存管理</h3><p>构造函数和析构函数让您可以钩子到对象的生命周期中。</p>
<p>通过将指针包装在对象中，可以在对象被销毁时释放内存。编译器保证这种情况发生，即使引发了异常。</p>
<p>这通常称为资源获取初始化(RAII)，并为您提供智能指针。</p>
<h4 id="C-例子"><a href="#C-例子" class="headerlink" title="C++例子"></a>C++例子</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">say_hello</span><span class="params">(std::unique_ptr&lt;Person&gt; person)</span> </span>&#123;</span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;Hello &quot;</span> &lt;&lt; person-&gt;name &lt;&lt; std::endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>std::unique_ptr</code>对象分配在栈上，指向分配在堆上的内存。</li>
<li>在<code>say_hello</code>结束时，<code>std::unique_ptr</code>析构函数将运行。</li>
<li>析构函数释放它所指向的<code>Person</code>对象。</li>
</ul>
<p>在将所有权传递给函数时，使用特殊的move构造函数:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">std::unique_ptr&lt;Person&gt; person = <span class="built_in">find_person</span>(<span class="string">&quot;Carla&quot;</span>);</span><br><span class="line"><span class="built_in">say_hello</span>(std::<span class="built_in">move</span>(person));</span><br></pre></td></tr></table></figure>

<h3 id="自动内存管理"><a href="#自动内存管理" class="headerlink" title="自动内存管理"></a>自动内存管理</h3><p>手动和基于作用域的内存管理的替代方案是自动内存管理</p>
<ul>
<li>程序员从不显式地分配或释放内存。</li>
<li>垃圾收集器找到未使用的内存并为程序员释放它。</li>
</ul>
<h4 id="Java例子"><a href="#Java例子" class="headerlink" title="Java例子"></a>Java例子</h4><p>在<code>sayHello</code>返回后，<code>person</code>对象没有被释放:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">sayHello</span><span class="params">(Person person)</span> &#123;</span><br><span class="line">  System.out.println(<span class="string">&quot;Hello &quot;</span> + person.getName());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Rust中的内存管理"><a href="#Rust中的内存管理" class="headerlink" title="Rust中的内存管理"></a>Rust中的内存管理</h3><p>Rust中的内存管理是一种混合模式:</p>
<ul>
<li>像Java一样安全正确，但是没有垃圾收集器。</li>
<li>像C++一样基于作用域，但是编译器强制完全遵循。</li>
<li>没有像C和C++那样的运行时开销。</li>
</ul>
<p>它通过显式地对所有权建模来实现这一点。</p>
<h2 id="对比"><a href="#对比" class="headerlink" title="对比"></a>对比</h2><p>下面是内存管理技术的一个粗略比较。</p>
<h3 id="不同内存管理技术的优点"><a href="#不同内存管理技术的优点" class="headerlink" title="不同内存管理技术的优点"></a>不同内存管理技术的优点</h3><ul>
<li>像 C 一样手动<ul>
<li>无运行时开销。</li>
</ul>
</li>
<li>像Java一样自动<ul>
<li>全自动的</li>
<li>安全且正确。</li>
</ul>
</li>
<li>基于作用域，如C++<ul>
<li>部分自动</li>
<li>无运行时开销。</li>
</ul>
</li>
<li>编译器强制的基于作用域的，像Rust<ul>
<li>由编译器强制执行。</li>
<li>无运行时开销。</li>
<li>安全且正确。</li>
</ul>
</li>
</ul>
<h3 id="不同内存管理技术的缺点"><a href="#不同内存管理技术的缺点" class="headerlink" title="不同内存管理技术的缺点"></a>不同内存管理技术的缺点</h3><ul>
<li>像 C 一样手动<ul>
<li>free后使用。</li>
<li>重复free</li>
<li>内存泄漏</li>
</ul>
</li>
<li>像Java一样自动<ul>
<li>垃圾收集时程序暂停。</li>
<li>析构函数延迟。</li>
</ul>
</li>
<li>基于作用域，如C++<ul>
<li>复杂，由程序员选择。</li>
<li>存在free后使用。</li>
</ul>
</li>
<li>编译器强制的基于作用域的，像Rust<ul>
<li>一些前期的复杂性。（应该是前期学习上比较难？）</li>
<li>可以拒绝有效的程序。（应该是可以拒绝编译器进行安全检查）</li>
</ul>
</li>
</ul>
<h2 id="所有权"><a href="#所有权" class="headerlink" title="所有权"></a>所有权</h2><p>所有变量绑定都有一个有效的作用域，使用超出其作用域的变量是错误的</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span>(<span class="type">i32</span>, <span class="type">i32</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p</span> = <span class="title function_ invoke__">Point</span>(<span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;x: &#123;&#125;&quot;</span>, p.<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;y: &#123;&#125;&quot;</span>, p.<span class="number">1</span>); <span class="comment">// 报错：not found in this scope</span></span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<ul>
<li>在作用域的末尾，删除变量并释放数据。</li>
<li>析构函数可以在这里运行以释放资源。</li>
<li>我们说变量拥有值。</li>
</ul>
<h3 id="转移语义"><a href="#转移语义" class="headerlink" title="转移语义"></a>转移语义</h3><p>赋值操作将在变量之间转移所有权</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">s1</span>: <span class="type">String</span> = <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Hello!&quot;</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">s2</span>: <span class="type">String</span> = s1;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;s2: &#123;s2&#125;&quot;</span>);</span><br><span class="line">    <span class="comment">// println!(&quot;s1: &#123;s1&#125;&quot;); // 报错：value borrowed here after move</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>s1</code>到<code>s2</code>的分配转移了所有权。</li>
<li>数据从<code>s1</code>移动，<code>s1</code>不再可访问。</li>
<li>当<code>s1</code>超出作用域时，什么都不会发生:它没有所有权。</li>
<li>当<code>s2</code>超出作用域时，字符串数据被释放。</li>
<li>总是只有一个变量绑定拥有一个值。</li>
</ul>
<h3 id="在-Rust-中移动的字符串"><a href="#在-Rust-中移动的字符串" class="headerlink" title="在 Rust 中移动的字符串"></a>在 Rust 中移动的字符串</h3><figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">s1</span>: <span class="type">String</span> = <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Rust&quot;</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">s2</span>: <span class="type">String</span> = s1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>s1</code>中的堆数据被<code>s2</code>重用。</li>
<li>当s1超出作用域时，什么都不会发生(它已被移出)。</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://google.github.io/comprehensive-rust/ownership/moved-strings-rust.html">这里有个图</a></p>
<h4 id="在现代C-中重复free"><a href="#在现代C-中重复free" class="headerlink" title="在现代C++中重复free"></a>在现代C++中重复free</h4><p>现代C++以不同的方式解决这个问题：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">std::string s1 = <span class="string">&quot;Cpp&quot;</span>;</span><br><span class="line">std::string s2 = s1;  <span class="comment">// Duplicate the data in s1.</span></span><br></pre></td></tr></table></figure>

<ul>
<li>s1中的堆数据被复制，s2得到它自己的独立副本。</li>
<li>当<code>s1</code>和<code>s2</code>超出作用域时，它们各自释放自己的内存。</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://google.github.io/comprehensive-rust/ownership/double-free-modern-cpp.html">这里有个图</a></p>
<h3 id="函数调用中的移动"><a href="#函数调用中的移动" class="headerlink" title="函数调用中的移动"></a>函数调用中的移动</h3><p>将值传递给函数时，值被赋给函数形参。这就转移了所有权:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">say_hello</span>(name: <span class="type">String</span>) &#123;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Hello &#123;name&#125;&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">name</span> = <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Alice&quot;</span>);</span><br><span class="line">    <span class="title function_ invoke__">say_hello</span>(name);</span><br><span class="line">    <span class="comment">// say_hello(name); // 再次调用报错：value used here after move</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="复制与克隆"><a href="#复制与克隆" class="headerlink" title="复制与克隆"></a>复制与克隆</h3><p>虽然move语义是默认的，但默认情况下会复制某些类型</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">x</span> = <span class="number">42</span>;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">y</span> = x;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;x: &#123;x&#125;&quot;</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;y: &#123;y&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这些类型实现了Copy trait。</p>
<p>您可以选择自己的类型来使用复制语义：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Copy, Clone, Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span>(<span class="type">i32</span>, <span class="type">i32</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p1</span> = <span class="title function_ invoke__">Point</span>(<span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p2</span> = p1;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;p1: &#123;p1:?&#125;&quot;</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;p2: &#123;p2:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>赋值之后，p1和p2都拥有自己的数据。</li>
<li>我们还可以使用<code>p1.clone()</code>显式复制数据。</li>
</ul>
<h3 id="借用-Borrowing"><a href="#借用-Borrowing" class="headerlink" title="借用 &#x2F; Borrowing"></a>借用 &#x2F; Borrowing</h3><p>在调用函数时，可以让函数借用值，而不是转移所有权：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span>(<span class="type">i32</span>, <span class="type">i32</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">add</span>(p1: &amp;Point, p2: &amp;Point) <span class="punctuation">-&gt;</span> Point &#123;</span><br><span class="line">    <span class="title function_ invoke__">Point</span>(p1.<span class="number">0</span> + p2.<span class="number">0</span>, p1.<span class="number">1</span> + p2.<span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p1</span> = <span class="title function_ invoke__">Point</span>(<span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p2</span> = <span class="title function_ invoke__">Point</span>(<span class="number">10</span>, <span class="number">20</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p3</span> = <span class="title function_ invoke__">add</span>(&amp;p1, &amp;p2);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;p1:?&#125; + &#123;p2:?&#125; = &#123;p3:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>add函数借用两个点并返回一个新点。</li>
<li>调用方保留输入的所有权。</li>
</ul>
<h4 id="共享和独特的借用"><a href="#共享和独特的借用" class="headerlink" title="共享和独特的借用"></a>共享和独特的借用</h4><p>Rust限制了借用值的方式：</p>
<ul>
<li>在任何给定时间都可以有一个或多个&amp;T值，或者</li>
<li>你只能有一个<code>&amp;mut</code>值。</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">a</span>: <span class="type">i32</span> = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">b</span>: &amp;<span class="type">i32</span> = &amp;a;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">c</span>: &amp;<span class="keyword">mut</span> <span class="type">i32</span> = &amp;<span class="keyword">mut</span> a; <span class="comment">// 报错：mutable borrow occurs here</span></span><br><span class="line">        *c = <span class="number">20</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;a: &#123;a&#125;&quot;</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;b: &#123;b&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="生命周期"><a href="#生命周期" class="headerlink" title="生命周期"></a>生命周期</h3><p>借来的值是有生命周期的：</p>
<ul>
<li>寿命可以省略: <code>add(p1: &amp;Point, p2: &amp;Point) -&gt; Point</code></li>
<li>生命周期也可以是明确的: <code>&amp;&#39;a Point</code>, <code>&amp;&#39;document str</code></li>
<li>读取 <code>&amp;&#39;a Point</code> 如同 “在a的最后生命周期前 a 借用 <code>Point</code>是有效的”</li>
<li>生命周期总是由编译器推断出来的:你不能自己分配一个生命周期。<ul>
<li>生命周期注解创建约束;编译器验证是否存在有效的解决方案。（应该是说可以用注解控制生命周期）</li>
</ul>
</li>
</ul>
<h3 id="函数调用中的生存期"><a href="#函数调用中的生存期" class="headerlink" title="函数调用中的生存期"></a>函数调用中的生存期</h3><p>除了借用实参外，函数还可以返回一个借来的值:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span>(<span class="type">i32</span>, <span class="type">i32</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">left_most</span>&lt;<span class="symbol">&#x27;a</span>&gt;(p1: &amp;<span class="symbol">&#x27;a</span> Point, p2: &amp;<span class="symbol">&#x27;a</span> Point) <span class="punctuation">-&gt;</span> &amp;<span class="symbol">&#x27;a</span> Point &#123;</span><br><span class="line">    <span class="keyword">if</span> p1.<span class="number">0</span> &lt; p2.<span class="number">0</span> &#123; p1 &#125; <span class="keyword">else</span> &#123; p2 &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p1</span>: Point = <span class="title function_ invoke__">Point</span>(<span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p2</span>: Point = <span class="title function_ invoke__">Point</span>(<span class="number">20</span>, <span class="number">20</span>);  <span class="comment">// Put into different scope</span></span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p3</span>: &amp;Point = <span class="title function_ invoke__">left_most</span>(&amp;p1, &amp;p2);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;left-most point: &#123;:?&#125;&quot;</span>, p3);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>&#39;a</code>是一个泛型参数，由编译器推断。</li>
<li>生命周期以<code>&#39;</code>和<code>&#39;a</code>开头，a是典型的默认名称。</li>
<li>读取 <code>&amp;&#39;a Point</code> 如同 “在a的最后生命周期前 a 借用 <code>Point</code>是有效的”<ul>
<li>当参数在不同的作用域时，最后部分是重要的。</li>
</ul>
</li>
</ul>
<h3 id="数据结构（Data-Structures）中的生命周期"><a href="#数据结构（Data-Structures）中的生命周期" class="headerlink" title="数据结构（Data Structures）中的生命周期"></a>数据结构（Data Structures）中的生命周期</h3><p>如果数据类型存储借来的数据，则必须使用生命期对其进行注释</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Highlight</span>&lt;<span class="symbol">&#x27;doc</span>&gt;(&amp;<span class="symbol">&#x27;doc</span> <span class="type">str</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">erase</span>(text: <span class="type">String</span>) &#123;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Bye &#123;text&#125;!&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">text</span> = <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;The quick brown fox jumps over the lazy dog.&quot;</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">fox</span> = <span class="title function_ invoke__">Highlight</span>(&amp;text[<span class="number">4</span>..<span class="number">19</span>]);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">dog</span> = <span class="title function_ invoke__">Highlight</span>(&amp;text[<span class="number">35</span>..<span class="number">43</span>]);</span><br><span class="line">    <span class="comment">// erase(text); // 报错：move out of `text` occurs here</span></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;fox:?&#125;&quot;</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;dog:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>我对所有权的个人理解：</p>
</blockquote>
<ul>
<li>Rust 中的每个堆内存只能由一个栈函数里的变量拥有，称为其所有者。</li>
<li>而且一次只能有一个所有者。</li>
<li>当所有者不在程序运行范围时（比如函数结束），该值将被删除。</li>
<li>如果其他函数想读写传进来的引用地址对应的值，可以使用<strong>借用</strong>，这样那个函数结束时不会释放内存，上一个函数也能够继续使用。</li>
</ul>
<p>举例就是这样</p>
<figure class="highlight rs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">s</span> = <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    <span class="comment">// s 被声明有效</span></span><br><span class="line"></span><br><span class="line">    <span class="title function_ invoke__">takes_ownership</span>(s);</span><br><span class="line">    <span class="comment">// s 的值被当作参数传入函数</span></span><br><span class="line">    <span class="comment">// 所以可以当作 s 已经被移动，从这里开始已经无效</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> <span class="variable">x</span> = <span class="number">5</span>;</span><br><span class="line">    <span class="comment">// x 被声明有效</span></span><br><span class="line"></span><br><span class="line">    <span class="title function_ invoke__">makes_copy</span>(x);</span><br><span class="line">    <span class="comment">// x 的值被当作参数传入函数</span></span><br><span class="line">    <span class="comment">// 但 x 是基本类型，依然有效</span></span><br><span class="line">    <span class="comment">// 在这里依然可以使用 x 却不能使用 s</span></span><br><span class="line"></span><br><span class="line">&#125; <span class="comment">// 函数结束, x 无效, 然后是 s. 但 s 已被移动, 所以不用被释放</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">takes_ownership</span>(some_string: <span class="type">String</span>) &#123;</span><br><span class="line">    <span class="comment">// 一个 String 参数 some_string 传入，有效</span></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;&#125;&quot;</span>, some_string);</span><br><span class="line">&#125; <span class="comment">// 函数结束, 参数 some_string 在这里释放</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">makes_copy</span>(some_integer: <span class="type">i32</span>) &#123;</span><br><span class="line">    <span class="comment">// 一个 i32 参数 some_integer 传入，有效</span></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;&#125;&quot;</span>, some_integer);</span><br><span class="line">&#125; <span class="comment">// 函数结束, 参数 some_integer 是基本类型, 无需释放</span></span><br></pre></td></tr></table></figure>

<h2 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h2><h3 id="设计库"><a href="#设计库" class="headerlink" title="设计库"></a>设计库</h3><p>明天我们将学习更多关于结构体和<code>Vec&lt;T&gt;</code>类型的知识。现在，您只需要知道它的API的一部分:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">vec</span> = <span class="built_in">vec!</span>[<span class="number">10</span>, <span class="number">20</span>];</span><br><span class="line">    vec.<span class="title function_ invoke__">push</span>(<span class="number">30</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;middle value: &#123;&#125;&quot;</span>, vec[vec.<span class="title function_ invoke__">len</span>() / <span class="number">2</span>]);</span><br><span class="line">    <span class="keyword">for</span> <span class="variable">item</span> <span class="keyword">in</span> vec.<span class="title function_ invoke__">iter</span>() &#123;</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;item: &#123;item&#125;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用它来创建库应用程序。复制下面代码并完善它：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// <span class="doctag">TODO:</span> remove this when you&#x27;re done with your implementation.</span></span><br><span class="line"><span class="meta">#![allow(unused_variables, dead_code)]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Library</span> &#123;</span><br><span class="line">    books: <span class="type">Vec</span>&lt;Book&gt;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    title: <span class="type">String</span>,</span><br><span class="line">    year: <span class="type">u16</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    <span class="comment">// This is a constructor, used below.</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">new</span>(title: &amp;<span class="type">str</span>, year: <span class="type">u16</span>) <span class="punctuation">-&gt;</span> Book &#123;</span><br><span class="line">        Book &#123;</span><br><span class="line">            title: <span class="type">String</span>::<span class="title function_ invoke__">from</span>(title),</span><br><span class="line">            year,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// This makes it possible to print Book values with &#123;&#125;.</span></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">std</span>::fmt::Display <span class="keyword">for</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">fmt</span>(&amp;<span class="keyword">self</span>, f: &amp;<span class="keyword">mut</span> std::fmt::Formatter&lt;<span class="symbol">&#x27;_</span>&gt;) <span class="punctuation">-&gt;</span> std::fmt::<span class="type">Result</span> &#123;</span><br><span class="line">        <span class="built_in">write!</span>(f, <span class="string">&quot;&#123;&#125; (&#123;&#125;)&quot;</span>, <span class="keyword">self</span>.title, <span class="keyword">self</span>.year)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Library</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">new</span>() <span class="punctuation">-&gt;</span> Library &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//fn len(self) -&gt; usize &#123;</span></span><br><span class="line">    <span class="comment">//    unimplemented!()</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//fn is_empty(self) -&gt; bool &#123;</span></span><br><span class="line">    <span class="comment">//    unimplemented!()</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//fn add_book(self, book: Book) &#123;</span></span><br><span class="line">    <span class="comment">//    unimplemented!()</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//fn print_books(self) &#123;</span></span><br><span class="line">    <span class="comment">//    unimplemented!()</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//fn oldest_book(self) -&gt; Option&lt;&amp;Book&gt; &#123;</span></span><br><span class="line">    <span class="comment">//    unimplemented!()</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="comment">// This shows the desired behavior. Uncomment the code below and</span></span><br><span class="line">    <span class="comment">// implement the missing methods. You will need to update the</span></span><br><span class="line">    <span class="comment">// method signatures, including the &quot;self&quot; parameter!</span></span><br><span class="line">    <span class="keyword">let</span> <span class="variable">library</span> = Library::<span class="title function_ invoke__">new</span>();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//println!(&quot;Our library is empty: &#123;&#125;&quot;, library.is_empty());</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">//library.add_book(Book::new(&quot;Lord of the Rings&quot;, 1954));</span></span><br><span class="line">    <span class="comment">//library.add_book(Book::new(&quot;Alice&#x27;s Adventures in Wonderland&quot;, 1865));</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">//library.print_books();</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">//match library.oldest_book() &#123;</span></span><br><span class="line">    <span class="comment">//    Some(book) =&gt; println!(&quot;My oldest book is &#123;book&#125;&quot;),</span></span><br><span class="line">    <span class="comment">//    None =&gt; println!(&quot;My library is empty!&quot;),</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">//println!(&quot;Our library has &#123;&#125; books&quot;, library.len());</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我的答案:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Library</span> &#123;</span><br><span class="line">    books: <span class="type">Vec</span>&lt;Book&gt;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    title: <span class="type">String</span>,</span><br><span class="line">    year: <span class="type">u16</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    <span class="comment">// This is a constructor, used below.</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">new</span>(title: &amp;<span class="type">str</span>, year: <span class="type">u16</span>) <span class="punctuation">-&gt;</span> Book &#123;</span><br><span class="line">        Book &#123;</span><br><span class="line">            title: <span class="type">String</span>::<span class="title function_ invoke__">from</span>(title),</span><br><span class="line">            year,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// This makes it possible to print Book values with &#123;&#125;.</span></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">std</span>::fmt::Display <span class="keyword">for</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">fmt</span>(&amp;<span class="keyword">self</span>, f: &amp;<span class="keyword">mut</span> std::fmt::Formatter&lt;<span class="symbol">&#x27;_</span>&gt;) <span class="punctuation">-&gt;</span> std::fmt::<span class="type">Result</span> &#123;</span><br><span class="line">        <span class="built_in">write!</span>(f, <span class="string">&quot;&#123;&#125; (&#123;&#125;)&quot;</span>, <span class="keyword">self</span>.title, <span class="keyword">self</span>.year)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Library</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">new</span>() <span class="punctuation">-&gt;</span> Library &#123;</span><br><span class="line">        Library &#123;</span><br><span class="line">            books: <span class="built_in">vec!</span>[]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">is_empty</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">bool</span> &#123;</span><br><span class="line">       <span class="keyword">self</span>.books.<span class="title function_ invoke__">is_empty</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">add_book</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, book: Book) &#123;</span><br><span class="line">       <span class="keyword">self</span>.books.<span class="title function_ invoke__">push</span>(book)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">print_books</span>(&amp;<span class="keyword">self</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> <span class="variable">item</span> <span class="keyword">in</span> <span class="keyword">self</span>.books.<span class="title function_ invoke__">iter</span>() &#123;</span><br><span class="line">            <span class="built_in">println!</span>(<span class="string">&quot;book: &#123;item&#125;&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">len</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">usize</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>.books.<span class="title function_ invoke__">len</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">oldest_book</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">Option</span>&lt;&amp;Book&gt; &#123;</span><br><span class="line">        <span class="keyword">self</span>.books.<span class="title function_ invoke__">iter</span>().<span class="title function_ invoke__">min_by_key</span>(|x| x.year)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// This shows the desired behavior. Uncomment the code below and</span></span><br><span class="line"><span class="comment">// implement the missing methods. You will need to update the</span></span><br><span class="line"><span class="comment">// method signatures, including the &quot;self&quot; parameter! You may</span></span><br><span class="line"><span class="comment">// also need to update the variable bindings within main.</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">library</span> = Library::<span class="title function_ invoke__">new</span>();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Our library is empty: &#123;&#125;&quot;</span>, library.<span class="title function_ invoke__">is_empty</span>());</span><br><span class="line"></span><br><span class="line">    library.<span class="title function_ invoke__">add_book</span>(Book::<span class="title function_ invoke__">new</span>(<span class="string">&quot;Lord of the Rings&quot;</span>, <span class="number">1954</span>));</span><br><span class="line">    library.<span class="title function_ invoke__">add_book</span>(Book::<span class="title function_ invoke__">new</span>(<span class="string">&quot;Alice&#x27;s Adventures in Wonderland&quot;</span>, <span class="number">1865</span>));</span><br><span class="line"></span><br><span class="line">    library.<span class="title function_ invoke__">print_books</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">match</span> library.<span class="title function_ invoke__">oldest_book</span>() &#123;</span><br><span class="line">       <span class="title function_ invoke__">Some</span>(book) =&gt; <span class="built_in">println!</span>(<span class="string">&quot;My oldest book is &#123;book&#125;&quot;</span>),</span><br><span class="line">       <span class="literal">None</span> =&gt; <span class="built_in">println!</span>(<span class="string">&quot;My library is empty!&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Our library has &#123;&#125; books&quot;</span>, library.<span class="title function_ invoke__">len</span>());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="迭代器和所有权-难"><a href="#迭代器和所有权-难" class="headerlink" title="迭代器和所有权(难)"></a>迭代器和所有权(难)</h3><p>Rust的所有权模型影响许多api。一个例子就是<a target="_blank" rel="noopener" href="https://doc.rust-lang.org/std/iter/trait.Iterator.html"><code>Iterator</code></a>和<a target="_blank" rel="noopener" href="https://doc.rust-lang.org/std/iter/trait.IntoIterator.html"><code>IntoIterator</code></a> traits。</p>
<h4 id="Iterator"><a href="#Iterator" class="headerlink" title="Iterator"></a>Iterator</h4><p>Traits 类似于接口:它们描述类型的行为(方法)。<code>Iterator</code> trait只是说，您可以调用<code>next</code>，直到返回<code>None</code>为止:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="keyword">trait</span> <span class="title class_">Iterator</span> &#123;</span><br><span class="line">    <span class="keyword">type</span> <span class="title class_">Item</span>;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">next</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">Option</span>&lt;<span class="keyword">Self</span>::Item&gt;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>你可以这样使用这个trait:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">v</span>: <span class="type">Vec</span>&lt;<span class="type">i8</span>&gt; = <span class="built_in">vec!</span>[<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>];</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">iter</span> = v.<span class="title function_ invoke__">iter</span>();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;v[0]: &#123;:?&#125;&quot;</span>, iter.<span class="title function_ invoke__">next</span>());</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;v[1]: &#123;:?&#125;&quot;</span>, iter.<span class="title function_ invoke__">next</span>());</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;v[2]: &#123;:?&#125;&quot;</span>, iter.<span class="title function_ invoke__">next</span>());</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;No more items: &#123;:?&#125;&quot;</span>, iter.<span class="title function_ invoke__">next</span>());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>迭代器返回的类型是什么?在这里测试你的答案:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">v</span>: <span class="type">Vec</span>&lt;<span class="type">i8</span>&gt; = <span class="built_in">vec!</span>[<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>];</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">iter</span> = v.<span class="title function_ invoke__">iter</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> <span class="variable">v0</span>: <span class="type">Option</span>&lt;..&gt; = iter.<span class="title function_ invoke__">next</span>();</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;v0: &#123;v0:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>为什么使用这种类型？</p>
<h4 id="IntoIterator"><a href="#IntoIterator" class="headerlink" title="IntoIterator"></a>IntoIterator</h4><p><code>Iterator</code> traut告诉您在创建迭代器后如何进行<em>迭代</em>。相关的trait <code>IntoIterator</code>告诉你如何创建迭代器:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="keyword">trait</span> <span class="title class_">IntoIterator</span> &#123;</span><br><span class="line">    <span class="keyword">type</span> <span class="title class_">Item</span>;</span><br><span class="line">    <span class="keyword">type</span> <span class="title class_">IntoIter</span>: <span class="built_in">Iterator</span>&lt;Item = <span class="keyword">Self</span>::Item&gt;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">into_iter</span>(<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="keyword">Self</span>::IntoIter;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里的语法意味着<code>IntoIterator</code>的每个实现都必须声明两种类型:</p>
<ul>
<li><code>Item</code>:迭代的类型，比如<code>i8</code></li>
<li><code>IntoIter</code>:由<code>into_iter</code>方法返回的<code>Iterator</code>类型。</li>
</ul>
<p>注意，IntoIter和Item是关联的:迭代器必须具有相同的<code>Item</code>类型，这意味着它返回<code>Option&lt;Item&gt;</code></p>
<p>和前面一样，迭代器返回的类型是什么?</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">v</span>: <span class="type">Vec</span>&lt;<span class="type">String</span>&gt; = <span class="built_in">vec!</span>[<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;foo&quot;</span>), <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;bar&quot;</span>)];</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">iter</span> = v.<span class="title function_ invoke__">into_iter</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> <span class="variable">v0</span>: <span class="type">Option</span>&lt;..&gt; = iter.<span class="title function_ invoke__">next</span>();</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;v0: &#123;v0:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h4><p>现在我们知道了<code>Iterator</code>和<code>IntoIterator</code>，我们可以构建<code>for</code>循环。它们在表达式上调用<code>into_iter()</code>并遍历得到的迭代器:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">v</span>: <span class="type">Vec</span>&lt;<span class="type">String</span>&gt; = <span class="built_in">vec!</span>[<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;foo&quot;</span>), <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;bar&quot;</span>)];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> <span class="variable">word</span> <span class="keyword">in</span> &amp;v &#123;</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;word: &#123;word&#125;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> <span class="variable">word</span> <span class="keyword">in</span> v &#123;</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;word: &#123;word&#125;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>每个循环中<code>word</code>的类型是什么?</p>
<blockquote>
<p>我的想法：如果是for in遍历变量的引用，那只是借用，而for in变量变量本身则会转移所有权，所有第一次循环的是&amp;String类型，第二次是String类型。（复制到ide就能看到类型）</p>
</blockquote>
<p>尝试上面的代码，然后查阅文档中的<a target="_blank" rel="noopener" href="https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-IntoIterator-2"><code>impl IntoIterator for &amp;Vec&lt;T&gt;</code></a>和<a target="_blank" rel="noopener" href="https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-IntoIterator-1"><code>impl IntoIterator for Vec&lt;T&gt;</code></a>来检查您的答案。</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/Rust/rust-note-2-google/" title="rust学习笔记（2）：Day1 Afternoon">https://wyatex.online/Rust/rust-note-2-google/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"># 学习笔记</a>
              <a href="/tags/Rust/" rel="tag"># Rust</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/Rust/rust-note-1-google/" rel="prev" title="rust学习笔记（1）：Day1 Moring">
                  <i class="fa fa-angle-left"></i> rust学习笔记（1）：Day1 Moring
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E8%AF%AD%E8%A8%80%E5%A4%A7%E6%9D%82%E7%83%A9%EF%BC%881%EF%BC%89/" rel="next" title="语言大杂烩（1）">
                  语言大杂烩（1） <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
